﻿Imports Co9320.NativeMethods

Public Class SoundManager
    Implements IDisposable

    Private Const SoundAliasName As String = "NamaAlertSound"
    Private Shared IsOpened As Boolean

    Private Sub New()
    End Sub

    Private Shared _settings As SoundSettings
    Public Shared Sub SetSettings(ByVal settings As SoundSettings)
        _settings = settings
    End Sub

    Private Shared Sub StopSound()
        Dim cmd As String
        ' 再生停止
        cmd = "stop " & SoundAliasName
        mciSendString(cmd, Nothing, IntPtr.Zero, IntPtr.Zero)
        ' ファイルを閉じる
        cmd = "close " & SoundAliasName
        mciSendString(cmd, Nothing, IntPtr.Zero, IntPtr.Zero)

        IsOpened = False
    End Sub

    ' 
    'mciSendString("setaudio my_sound volume to 1000", null, 0, IntPtr.Zero)

    Public Shared Sub SetVolume(ByVal value As Integer)
        If 0 <= value AndAlso value <= 10000 Then
            mciSendString("setaudio my_sound volume to ", Nothing, IntPtr.Zero, IntPtr.Zero)
        End If
    End Sub


    Public Shared Sub PlaySound(ByVal file As String)
        Try
            If Not System.IO.File.Exists(file.Trim) Then
                Exit Sub
            Else
                file = file.Trim
            End If

            Dim cmd As String

            If IsOpened Then
                StopSound()
            End If

            Dim ext = System.IO.Path.GetExtension(file).ToLower
            If ext = ".mp3" Then
                cmd = "open """ + file + """ type mpegvideo alias " & SoundAliasName
            Else
                cmd = "open """ + file + """ alias " & SoundAliasName
            End If

            ' ファイルを開く
            If mciSendString(cmd, Nothing, IntPtr.Zero, IntPtr.Zero) <> 0 Then
                Exit Sub
            End If

            '再生
            cmd = "play " & SoundAliasName
            IsOpened = True
            mciSendString(cmd, Nothing, IntPtr.Zero, IntPtr.Zero)



            'Dim ext = System.IO.Path.GetExtension(file).ToLower
            'Select Case ext
            '    Case ".wav"

            '        Dim player = New System.Media.SoundPlayer
            '        player.SoundLocation = file
            '        player.Play()
            '        player.Dispose()


            '    Case Else



            'End Select
        Catch ex As Exception
            ' Ignore
        End Try
    End Sub

    Public Shared Sub PlaySound()
        If _settings IsNot Nothing Then
            PlaySound(_settings.Path)
        End If
    End Sub

#Region "IDisposable"

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If

            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

#End Region

End Class
